import { getUserInfo } from '@/api/admin'
import { login } from '@/api/login'
import { countOrderLog } from '@/api/order'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { welcome } from '@/utils/util'
import storage from 'store'

const user = {
    state: {
        token: '',
        name: '',
        welcome: '',
        avatar: '',
        roles: [],
        info: {},
        count: 0
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, { name, welcome }) => {
            state.name = name
            state.welcome = welcome
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_INFO: (state, info) => {
            state.info = info
        },
        SET_COUNT: (state, count) => {
            state.count = count
        }
    },

    actions: {
        // 登录
        Login({ commit }, userInfo) {
            return new Promise((resolve, reject) => {
                login(userInfo)
                    .then(response => {
                        const result = response.data
                        storage.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
                        commit('SET_TOKEN', result.token)
                        this.dispatch('Count')
                        resolve()
                    })
                    .catch(error => {
                        reject(error)
                    })
            })
        },
        // 获取用户信息
        GetInfo({ commit }) {
            return new Promise((resolve, reject) => {
                this.dispatch('Count')
                getUserInfo()
                    .then(response => {
                        const result = response.data
                        if (result.role) {
                            // const role = result.role_menu
                            // role.permissions = result.role.permissions
                            // role.permissions.map(per => {
                            //     if (per.actionEntitySet != null && per.actionEntitySet.length > 0) {
                            //         const action = per.actionEntitySet.map(action => { return action.action })
                            //         per.actionList = action
                            //     }
                            // })
                            // role.permissionList = role.permissions.map(permission => { return permission.permissionId })
                            commit('SET_ROLES', result.role)

                            commit('SET_INFO', result)
                        } else {
                            reject(new Error('getInfo: roles must be a non-null array !'))
                        }

                        commit('SET_NAME', { name: result.username, welcome: welcome() })

                        commit('SET_AVATAR', result.avatar)

                        commit('SET_INFO', result)

                        resolve(response)
                    })
                    .catch(error => {
                        reject(error)
                    })
            })
        },
        // 登出
        Logout({ commit }) {
            return new Promise(resolve => {
                commit('SET_TOKEN', '')
                commit('SET_ROLES', [])
                commit('SET_INFO', {})
                storage.remove(ACCESS_TOKEN)
                resolve()
                // logout(state.token).then(() => {
                //     commit('SET_TOKEN', '')
                //     commit('SET_ROLES', [])
                //     storage.remove(ACCESS_TOKEN)
                //     resolve()
                // }).catch(() => {
                //     resolve()
                // }).finally(() => {
                // })
            })
        },
        Count({ commit }) {
            return new Promise(resolve => {
                countOrderLog().then(res => {
                    const { data: { count } } = res
                    commit('SET_COUNT', count)
                    resolve()
                })
            })
        }
    }
}

export default user
